package com.yfq;

import com.yfq.springframework.beans.FactoryBean;
import com.yfq.springframework.util.ClassUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: yfq
 * @date: 2023/1/31 15:23
 * @description:
 */
public class IUserDaoFactoryBean implements FactoryBean<IUserDao> {
    @Override
    public IUserDao getObject() throws Exception {
        InvocationHandler handler = (proxy, method, args) -> {

            Map<String, String> hashMap = new HashMap<>();
            hashMap.put("10001", "小傅哥");
            hashMap.put("10002", "八杯水");
            hashMap.put("10003", "阿毛");

            return "你被代理了 " + method.getName() + "：" + hashMap.get(args[0].toString());
        };

        //返回IUserDao的代理对象
        return (IUserDao) Proxy.newProxyInstance(ClassUtils.getDefaultClassLoader(), new Class[]{getObjectType()}, handler);
    }

    @Override
    public Class<?> getObjectType() {
        return IUserDao.class;
    }
}
